﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;

using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Domain.Repositories;
using XCZ;
using Business.Models;
using Business.PricingManagement.Dto;
using Business.ProduceManagement.Dto;
using Microsoft.EntityFrameworkCore;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory;
using Business.PurchManagement.Dto;

namespace Business.PricingManagement
{
    public class PricingAppService : BusinessWorkFlowAppService, IPricingAppService
    {
        private const string FormName = "Pricing";
        private IRepository<Pricing, Guid> _repository;
        private IRepository<Produce, Guid> _repository1;

        public PricingAppService(
            IRepository<Pricing, Guid> repository,
            IRepository<Produce, Guid> repository1
            )
        {
            _repository = repository;
            _repository1 = repository1;
        }
        #region 增删改查基础方法

        public async Task<PricingDto> Get(Guid id)
        {
            var data = await _repository.GetAsync(id);
            var dto = ObjectMapper.Map<Pricing, PricingDto>(data);
            return dto;
        }

        public async Task<PagedResultDto<PricingDto>> GetAll(GetPricingInputDto input)
        {
            var query = (await _repository.GetQueryableAsync());


            var totalCount = await query.CountAsync();
            var items = await query.OrderBy(input.Sorting ?? "Id")
                        .ToListAsync();

            var dtos = ObjectMapper.Map<List<Pricing>, List<PricingDto>>(items);
            return new PagedResultDto<PricingDto>(totalCount, dtos);
        }

        public async Task<PagedResultDto<pingDto>> GetAlls(GetPricingInputDto input)
        {
            var list = (from a in await _repository.GetQueryableAsync()
                        join b in await _repository1.GetQueryableAsync()
                        on a.Id equals b.Id
                        select new pingDto
                        {
                            Id = a.Id,
                            Associated = a.Associated,
                            BillType = b.BillType,
                            Documentoforigin = a.Documentoforigin,
                            Documentsubject = a.Documentsubject,
                            EnquiryProject = a.EnquiryProject,
                            EnquiryTimes = a.EnquiryTimes,
                            PricingPeople = a.PricingPeople,
                            PricingTimes = a.PricingTimes,
                            ProCode = b.ProCode,
                            ProName = b.ProName,
                            ProPrice = b.ProPrice,
                            ProDeliverydate = b.ProDeliverydate,
                            ProDiscount = b.ProDiscount,
                            ProDiscountunit = b.ProDiscountunit,
                            ProIncludingtax = b.ProIncludingtax,
                            ProNumber = b.ProNumber,
                            ProRemark = b.ProRemark,
                            ProSumprice = b.ProSumprice,
                            ProTaxrate = b.ProTaxrate,
                            ProTheamount = b.ProTheamount,
                            ProUnitprice = b.ProUnitprice,
                            ProUntaxedamount = b.ProUntaxedamount,
                            ReceiptsCode = a.ReceiptsCode,
                            SourcereType = a.SourcereType,
                            SpecType = b.SpecType,
                            Unit = b.Unit,

                        }).ToList();
            var query = await _repository.GetQueryableAsync();
            var lists=await _repository1.GetQueryableAsync();

            //关联项目
            query = query.WhereIf(!string.IsNullOrWhiteSpace(input.EnquiryProject), a => a.EnquiryProject.Contains(input.EnquiryProject));

            //产品名称
            lists = lists.WhereIf(!string.IsNullOrWhiteSpace(input.ProName), a => a.ProName.Contains(input.ProName));

            //产品编码
            lists = lists.WhereIf(!string.IsNullOrWhiteSpace(input.ProCode), a => a.ProCode.Contains(input.ProCode));

            //定价编码
            query = query.WhereIf(!string.IsNullOrWhiteSpace(input.ReceiptsCode), a => a.ReceiptsCode.Contains(input.ReceiptsCode));

            //定价主题
            query = query.WhereIf(!string.IsNullOrWhiteSpace(input.Documentsubject), a => a.Documentsubject.Contains(input.Documentsubject));

            //定价人员
            query = query.WhereIf(!string.IsNullOrWhiteSpace(input.PricingPeople), a => a.PricingPeople.Contains(input.PricingPeople));

            //关联询价单
            query = query.WhereIf(!string.IsNullOrWhiteSpace(input.Associated), a => a.Associated.Contains(input.Associated));


            //日期
            query = query.WhereIf(!string.IsNullOrEmpty(input.StartDate), x => x.PricingTimes >= DateTime.Parse(input.StartDate));
            query = query.WhereIf(!string.IsNullOrEmpty(input.EndDate), x => x.PricingTimes <= DateTime.Parse(input.EndDate).AddDays(1));


            var totalCount = list.Count();
            list = list.Skip(input.SkipCount).Take(input.MaxResultCount).ToList();
            return new PagedResultDto<pingDto>(totalCount, list);
        }

        public async Task<PricingDto> CreateOrUpdate(CreateOrUpdatePricingDto input)
        {
            Pricing result = null;
            if (!input.Id.HasValue)
            {
                input.Id = GuidGenerator.Create();
                result = await _repository.InsertAsync(ObjectMapper.Map<CreateOrUpdatePricingDto, Pricing>(input));

                //创建工作流
                // await WorkFlow.CreateWorkFlow(FormName, result);
            }
            else
            {
                var data = await _repository.GetAsync(input.Id.Value);
                result = await _repository.UpdateAsync(ObjectMapper.Map(input, data));
                //检查流程状态
                //await WorkFlow.CheckFormStatus(input.Id.Value, result);
            }
            return ObjectMapper.Map<Pricing, PricingDto>(result);
        }

        public async Task Delete(List<Guid> ids)
        {
            foreach (var item in ids)
            {
                await _repository.DeleteAsync(item);
            }

        }
        #endregion

    }
}
